//
//  ST_BTTwicePickerView.swift
//  btvideo
//
//  Created by mac on 2019/11/7.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift

class ST_BTTwicePickerView: UIView {

    static let height = 250+BTSafeAreaBottom
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = Color.init(hexString: "#F2F4F7")
        
        self.addSubview(top_view)
        top_view.snp.makeConstraints({ (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(44)
        })
        
        self.addSubview(confirm_picker_btn)
        confirm_picker_btn.snp.makeConstraints({ (make) in
            make.right.equalTo(self).offset(-50)
            make.width.height.equalTo(44)
            make.top.equalTo(self)
        })
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints({ (make) in
            make.centerY.equalTo(confirm_picker_btn)
            make.centerX.equalToSuperview()
        })
        
        self.addSubview(cancel_picker_btn)
        cancel_picker_btn.snp.makeConstraints({ (make) in
            make.left.equalTo(self).offset(50)
            make.width.height.equalTo(44)
            make.top.equalTo(self)
        })
        
        self.addSubview(pickerView)
        pickerView.snp.makeConstraints({ (make) in
            make.top.equalTo(self.cancel_picker_btn.snp.bottom).offset(10)
            make.left.right.equalTo(self)
            make.bottom.equalTo(self).offset(-BTSafeAreaBottom)
        })
        
        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = Color.init(hexString: "#E7ECF4")
        line.snp.makeConstraints({ (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(1)
        })
        
        guard let idx = normalSelectedIndexPath else { return  }
        self.pickerView.reloadAllComponents()
        self.scrollTo(indexPath: idx)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    lazy var top_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "请选择"
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    lazy var confirm_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("完成")
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(confirm_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    lazy var cancel_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("取消")
        control.setTitleColorForAllStates(Color.init(hexString: "#999999")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.addTarget(self, action: #selector(cancel_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    

    
    lazy var pickerView:UIPickerView = {
        let control = UIPickerView()
        control.delegate = self
        control.dataSource = self
        return control
    }()
    
    
    var option_array = [ST_BTTwicePickerViewModel]()
    
    var normalSelectedIndexPath:IndexPath?
    
    fileprivate var selectedIndexPath = IndexPath.init(row: 0, section: 0)
    
//    MARK: - Event
    
    func scrollTo(indexPath:IndexPath){
        
        selectedIndexPath = indexPath
        
        self.pickerView.selectRow(indexPath.row, inComponent: indexPath.section, animated: true)
        
    }
    
    
    var confirm_pick_closure:((_ title:String,_ subTitle:String,_ indexPath:IndexPath)->())?
    
    @objc func confirm_picker_action(){
        
        if let title = self.option_array[safe:selectedIndexPath.section]?.title,let childTitle = self.option_array[safe:selectedIndexPath.section]?.child[safe:selectedIndexPath.row]?.title{
            
            confirm_pick_closure?(title,childTitle,selectedIndexPath)
        }
        
        
        cancel_picker_action()
    }
    
    
    @objc func cancel_picker_action(){
        
        self.isHidden = true
        
    }
    
}

extension ST_BTTwicePickerView:UIPickerViewDelegate,UIPickerViewDataSource{
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }

    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        if component == 0{
            return option_array.count
        }
        
        if let section = option_array[safe:selectedIndexPath.section]{
            
            return section.child.count
        }
        return 0
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        if component == 0{
            return option_array[safe:row]?.title ?? ""
        }else{
            return option_array[safe:selectedIndexPath.section]?.child[safe:row]?.title ?? ""
        }
    }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        
        
        if component == 0{
            selectedIndexPath = IndexPath.init(row: selectedIndexPath.row, section: row)
            pickerView.reloadComponent(1)
        }else{
            selectedIndexPath = IndexPath.init(row: row, section: selectedIndexPath.section)
        }
        
        
        
    }
    
}


//MARK: - Model
struct ST_BTTwicePickerViewModel {
    var title:String
    var child:[BTTwicePickerItemViewModel]
}

struct BTTwicePickerItemViewModel {
    var title:String
}
